package com.ytjj.qmyx.admin.service.imService.impl;

import com.ytjj.common.constants.im.ImRedisKeyConstant;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.mapper.ImConfigInfoMapper;
import com.ytjj.qmyx.admin.mapper.ImConfigValueInfoMapper;
import com.ytjj.qmyx.admin.model.dto.ImConfigValueInfoDto;
import com.ytjj.qmyx.admin.model.response.ImConfigValueInfoResponse;
import com.ytjj.qmyx.admin.model.vo.ImConfigValueInfoVO;
import com.ytjj.qmyx.admin.service.imService.ImConfigValueInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ImConfigValueInfoServiceImpl implements ImConfigValueInfoService {

    @Resource
    private ImConfigValueInfoMapper imConfigValueInfoMapper;
    @Resource
    private ImConfigInfoMapper imConfigInfoMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 修改设置
     * @param request
     * @return
     */
    @Override
    public int saveConfigValue(ImConfigValueInfoVO request) {
        request.setUpdateTime(new Date());
        Integer count = imConfigValueInfoMapper.update(request);
        return count;
    }

    /**
     * 获取设置详情
     * @return
     */
    @Override
    public ImConfigValueInfoResponse getConfigValue() {
        List<ImConfigValueInfoDto> list = imConfigValueInfoMapper.getConfigValue();
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        ImConfigValueInfoResponse response = new ImConfigValueInfoResponse();
        for (ImConfigValueInfoDto imConfigValueInfoDto : list) {
            switch (imConfigValueInfoDto.getConfigId()) {
                case 2:
                    response.setConfigKeyTwo(imConfigValueInfoDto);
                    break;
                case 3:
                    response.setConfigKeyThree(imConfigValueInfoDto);
                    break;
                case 4:
                    response.setConfigKeyFour(imConfigValueInfoDto);
                    break;
                case 5:
                    response.setConfigKeyFive(imConfigValueInfoDto);
                    break;
                case 6:
                    response.setConfigKeySix(imConfigValueInfoDto);
                    break;
                case 7:
                    response.setConfigKeySeven(imConfigValueInfoDto);
                    break;
                case 8:
                    response.setConfigKeyOne(imConfigValueInfoDto);
                    break;
                case 9:
                    response.setConfigKeyEight(imConfigValueInfoDto);
                    break;
                case 10:
                    response.setConfigKeynine(imConfigValueInfoDto);
                    break;
                case 11:
                    response.setConfigKey11(imConfigValueInfoDto);
                    break;
                case 12:
                    response.setConfigKey12(imConfigValueInfoDto);
                    break;
                case 13:
                    response.setConfigKey13(imConfigValueInfoDto);
                    break;
                case 14:
                    response.setConfigKey14(imConfigValueInfoDto);
                    break;
                case 15:
                    response.setConfigKey15(imConfigValueInfoDto);
                    break;
                case 16:
                    response.setConfigKey16(imConfigValueInfoDto);
                    break;

            }
        }
        return response;
    }

    @Override
    public int updateConfigValue(ImConfigValueInfoVO request) {
        request.setUpdateTime(new Date());
        Integer i = imConfigInfoMapper.updateById(request.getConfigId(), request.getOffFlag(), request.getUpdateUser());
        Integer count = imConfigValueInfoMapper.update(request);
        if (null != request.getConfigId() && 10 == request.getConfigId()) {
            //非服务时间
            redisService.set(ImRedisKeyConstant.OUT_OF_SERVICE_TIME_MESSAGE_SWITCH, request.getOffFlag() ? "1" : "0");
            if (request.getOffFlag()) {
                redisService.set(ImRedisKeyConstant.OUT_OF_SERVICE_TIME_MESSAGE, request.getContent());
            }else {
                redisService.remove(ImRedisKeyConstant.OUT_OF_SERVICE_TIME_MESSAGE);
            }
        } else if (null != request.getConfigId() && 5 == request.getConfigId()) {
            //客户欢迎消息
            redisService.set(ImRedisKeyConstant.WELCOME_MESSAGE_SWITCH, request.getOffFlag() ? "1" : "0");
            if (request.getOffFlag()) {
                redisService.set(ImRedisKeyConstant.WELCOME_MESSAGE, request.getContent());
            }else {
                redisService.remove(ImRedisKeyConstant.WELCOME_MESSAGE);
            }
        } else if (null != request.getConfigId() && 7 == request.getConfigId()) {
            //系统结束对话
            redisService.set(ImRedisKeyConstant.TO_CUSTOMER_SYS_MESSAGE_SWITCH, request.getOffFlag() ? "1" : "0");
            if (request.getOffFlag()) {
                redisService.set(ImRedisKeyConstant.TO_CUSTOMER_SYS_MESSAGE_BY_NOT_REPLY, request.getContent());
                redisService.set(ImRedisKeyConstant.NOT_REPLY_MESSAGE_TIME_CUSTOMER, String.valueOf(request.getThreshold()));
            }else {
                redisService.remove(ImRedisKeyConstant.TO_CUSTOMER_SYS_MESSAGE_BY_NOT_REPLY);
                redisService.remove(ImRedisKeyConstant.NOT_REPLY_MESSAGE_TIME_CUSTOMER);
            }
        }else if (null != request.getConfigId() && 6 == request.getConfigId()){
            //客服无应答时提示
            redisService.set(ImRedisKeyConstant.SERVICE_NO_ANSWER_SWITCH, request.getOffFlag() ? "1" : "0");
            if (request.getOffFlag()) {
                //如果为true,把回复内容存到redis中
                redisService.set(ImRedisKeyConstant.SERVICE_NO_ANSWER_MESSAGE, request.getContent());
            }else {
                //如果为false,把回复内容从redis中移除
                redisService.remove(ImRedisKeyConstant.SERVICE_NO_ANSWER_MESSAGE);
            }
        }else if (null != request.getConfigId() && 8 == request.getConfigId()) {
            //客服结束对话--回复内容
            redisService.set(ImRedisKeyConstant.END_MESSAGE, request.getContent());
        }else if (null != request.getConfigId() && 9 == request.getConfigId()) {
            //排队超时提醒--回复内容
            redisService.set(ImRedisKeyConstant.QUEUE_UP_TIMEOUT_MESSAGE, request.getContent());
        }
        return (i+count) ;
    }
}
